﻿using FineUI;
using SysManage.BLL;
using System;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Grassroots;
using ZHDJ.WebPage;

namespace SysManage.Module.Grassroots
{
    public partial class GroupReportEdit : PageBase
    {
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            ControlBusiness.ControlBind(ddlGroup, PartyGrassrootsGroup.GetGroupData("", IsSystemAdmin ? "" : this.BranchID), "GroupName", "ID", true);
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id != "")
            {
                PartyGrassrootsGroupReport report = DB.PartyGrassrootsGroupReports.Find(id);
                if (report == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                this.ddlGroup.SelectedValue = PartyGrassrootsGroupItem.QueryGroupIDByItemID(report.ItemID);
                ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ItemID");
                ddlItem.SelectedValue = report.ItemID;
                txtChief.Text = report.Chief;
                dpDate.Text = Convert.ToDateTime(report.ReportTime).ToString("yyyy-MM");
                txtjiedaiRenci.Text = report.jiedaiRenci.ToString();
                txtjiedaiJishu.Text = report.jiedaiJishu.ToString();
                txtjiedaiLu.Text = report.jiedaiLu.ToString();
                txtruhuHuci.Text = report.ruhuHuci.ToString();
                txtruhuJishu.Text = report.ruhuJishu.ToString();
                txtruhuLu.Text = report.ruhuLu.ToString();
                txtnianxika.Text = report.nianxika.ToString();
                txtjianyishu.Text = report.jianyishu.ToString();
                txtjiejueWentishu.Text = report.jiejueWentishu.ToString();
                txtRemark.Text = report.Remark;
            }
        }
        #endregion

        #region Events

        private void SaveData()
        {
            string id = GetQueryIntValue("id");

            if (id != "")
            {
                //更新
                PartyGrassrootsGroupReport report = DB.PartyGrassrootsGroupReports.Find(id);
                if (report == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                report.ItemID = ddlItem.SelectedValue;
                report.Chief = txtChief.Text.Trim();
                report.ReportTime = Convert.ToDateTime(dpDate.Text);
                report.jiedaiRenci = Convert.ToInt32(txtjiedaiRenci.Text.Trim());
                report.jiedaiJishu = Convert.ToInt32(txtjiedaiJishu.Text.Trim());
                report.jiedaiLu = Convert.ToInt32(txtjiedaiLu.Text.Trim());
                report.ruhuHuci = Convert.ToInt32(txtruhuHuci.Text.Trim());
                report.ruhuJishu = Convert.ToInt32(txtruhuJishu.Text.Trim());
                report.ruhuLu = Convert.ToInt32(txtruhuLu.Text.Trim());
                report.nianxika = Convert.ToInt32(txtnianxika.Text.Trim());
                report.jianyishu = Convert.ToInt32(txtjianyishu.Text.Trim());
                report.jiejueWentishu = Convert.ToInt32(txtjiejueWentishu.Text.Trim());
                report.Remark = txtRemark.Text.Trim();
                report.ModifyTime = DateTime.Now;
                report.ModifyBy = CurrentUser.LoginID;

                DB.SaveChanges();
            }
            else
            {
                PartyGrassrootsGroupReport report = new PartyGrassrootsGroupReport();
                report.ID = StringHelper.NewID;
                report.ItemID = ddlItem.SelectedValue;
                report.Chief = txtChief.Text.Trim();
                report.ReportTime = Convert.ToDateTime(dpDate.Text);
                report.jiedaiRenci = Convert.ToInt32(txtjiedaiRenci.Text.Trim());
                report.jiedaiJishu = Convert.ToInt32(txtjiedaiJishu.Text.Trim());
                report.jiedaiLu = Convert.ToInt32(txtjiedaiLu.Text.Trim());
                report.ruhuHuci = Convert.ToInt32(txtruhuHuci.Text.Trim());
                report.ruhuJishu = Convert.ToInt32(txtruhuJishu.Text.Trim());
                report.ruhuLu = Convert.ToInt32(txtruhuLu.Text.Trim());
                report.nianxika = Convert.ToInt32(txtnianxika.Text.Trim());
                report.jianyishu = Convert.ToInt32(txtjianyishu.Text.Trim());
                report.jiejueWentishu = Convert.ToInt32(txtjiejueWentishu.Text.Trim());
                report.Remark = txtRemark.Text.Trim();
                report.CreateTime = DateTime.Now;
                report.ModifyTime = DateTime.Now;
                report.CreateBy = CurrentUser.LoginID;
                report.ModifyBy = CurrentUser.LoginID;

                DB.PartyGrassrootsGroupReports.Add(report);

                DB.SaveChanges();
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }

        protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            ControlBusiness.ControlBind(ddlItem, PartyGrassrootsGroupItem.QueryItemDataByGroupID(ddlGroup.SelectedValue, IsSystemAdmin ? "" : this.BranchID), "ItemName", "ID", true);
        }
        #endregion
    }
}